#include "RSFMediaSource.h"
#include "RSF.h"
#include "RtpPacket.h"
#include "SSMonitorItems.h"

#define PRE_READ_SUM   1024

CRSFMediaSource::CRSFMediaSource():CMediaSource()
{
	m_pRsfCollector = 0;

//	m_pRsfCollector = new CRsfMediaCollector(inMovieName);

	Assert( m_pRsfCollector );

	_RtpInfo  rtp_info;
	CHANNEL_INFO_T track_info;
	UInt32 track_sum = m_pRsfCollector->GetChannelSum();
	for(UInt32 i=0;i<track_sum;i++)
	  {
	    m_pRsfCollector->GetChannelInfo( i,track_info );
	    rtp_info.TrackID = track_info.chnID;
	    rtp_info.SSRC = track_info.ssrc;
	    rtp_info.IsSet = FALSE;
	    m_RtpInfos[rtp_info.TrackID] = rtp_info;
	  }

	// read n packets data for fill rtp info
	UInt32  rtp_count = m_pRsfCollector->GetRTPCount();
	if( PRE_READ_SUM < rtp_count )
	  {
	    rtp_count = PRE_READ_SUM;
	  }

	UInt32  set_num=0;
	RFH_INDEX_ITEM_T* pItem;
	for( UInt32 j=0;j<rtp_count;j++)
	  {
	      pItem =  m_pRsfCollector->GetIndexItem( j );
	      if( pItem )
		{
		  _RtpInfosMapIterator it = m_RtpInfos.find(pItem->chnID );
		  if( !it->second.IsSet )
		    {
		      it->second.SeqNo = pItem->seqNo;
		      it->second.RtpTime = pItem->sendTime;
		      it->second.IsSet = TRUE;
		      if( ++set_num >= m_RtpInfos.size()) // all track have set
			break;
		    }
		}
	  }

	STAT_COUNT_INC(this);
}

CRSFMediaSource::~CRSFMediaSource()
{
	OutputDebugString("CRSFMediaSource::~CRSFMediaSource\n");
  if( m_pRsfCollector )
    {
		OutputDebugString("CRSFMediaSource::~CRSFMediaSource,delete m_pRsfCollector\n");
      delete m_pRsfCollector;
      m_pRsfCollector = 0;
    }

  STAT_COUNT_DEC(this);
	OutputDebugString("CRSFMediaSource::~CRSFMediaSource,exit\n");
}

Bool CRSFMediaSource::GetSDP( std::string * outSDP, const char * vLocalIP )
{		
  	return m_pRsfCollector->GetSDP(outSDP);
}

Bool CRSFMediaSource::AddTrack( UInt32 inTrackID, UInt32 & v_SSRC ) 
{
	_RtpInfosMapIterator it = m_RtpInfos.find(inTrackID);
	if(it != m_RtpInfos.end())
	{
		v_SSRC = it->second.SSRC;
		return TRUE;
	}
	else
		return FALSE;
}

Bool CRSFMediaSource::GetCurrentRtpInfo( UInt32 inTrackID, UInt16 &outSeqNo, UInt32 &outRtpTime ) 
{		
  _RtpInfosMapIterator it = m_RtpInfos.find(inTrackID);
  if( it != m_RtpInfos.end() )
    {
      if( it->second.IsSet )
	{
	  outSeqNo = it->second.SeqNo;
	  outRtpTime = it->second.RtpTime;
	  return TRUE;
	}
    }

  return FALSE;
}

ERRCODE	CRSFMediaSource::PreReadData( UInt32 v_Duration )
{
  m_pRsfCollector->ReadRsfData(m_ID,v_Duration,&m_RTPBuffer);

  return OS_NoErr;
}

ERRCODE	CRSFMediaSource::Seek(UInt64  vStartTime, Bool isSureKeyFrame)
{
  return m_pRsfCollector->Seek( vStartTime );
}
ERRCODE CRSFMediaSource::Seek2(UInt64  vStartTime,UInt32 test)
{
  return m_pRsfCollector->Seek( vStartTime );
}
ERRCODE	CRSFMediaSource::Pause()
{
  return m_pRsfCollector->Pause();
}
void CRSFMediaSource::FreeData()
{
  m_pRsfCollector->FreeData( m_ID );
}

